package com.jt.aop;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.SysResult;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

//全局异常处理
@RestControllerAdvice
public class SysResultException {
    @ExceptionHandler(RuntimeException.class)

    public Object fail(Exception e, HttpServletRequest request){
        String callback= request.getParameter("callback");
        if (StringUtils.hasLength(callback)){
            e.printStackTrace();
            return new JSONPObject(callback, SysResult.fail());
        }
        e.printStackTrace();
        return SysResult.fail();
    }
}
